package models;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import models.msgs.*;
import play.Logger;
import play.mvc.*;
import play.libs.*;
import play.libs.F.*;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
import akka.actor.*;
import akka.io.TcpPipelineHandler.Tell;
import static akka.pattern.Patterns.ask;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

import static java.util.concurrent.TimeUnit.*;

public class Human extends UntypedActor
{
	private int totalCash;
	private int entryFee;
	boolean ifChange;
    public final String               name;
    public final ActorRef             table;

    protected WebSocket.In<JsonNode>  in;
    protected WebSocket.Out<JsonNode> out;
    
    private static Hand  hand = new Hand();
    
    int i = 0;
    
    public Human(String _name, WebSocket.In<JsonNode> _in,
            WebSocket.Out<JsonNode> _out, ActorRef _table)
    {
        name = _name;        
        table = _table;
        in = _in;
        out = _out;
        
        
        
        in.onMessage(new Callback<JsonNode>()
        {
            @Override
            public void invoke(JsonNode event)
            {
                try
                {
                	String inText = event.get("nr").asText();      
                	//if(inText == "change")
                		
                		//getSelf().tell(new Change(inText, name), getSelf());
                		
                	//lse 
                		getSelf().tell(new Message(inText,name,totalCash), getSelf() );   
                	
                }
                catch (Exception e)
                {
                    Logger.error("invokeError");
                }
                
            }
        });

        in.onClose(new Callback0()
        {
            @Override
            public void invoke()
            {
                table.tell(new Quit(name), getSelf() );
            }
        });
    }

    @Override
    public void preStart()
    {        
        String text = "my Actor is now running!";
    	table.tell(new Info(text, name), getSelf()); 
    }

    @Override
    public void postStop()
    {
        String text = "I've been killed";
    	table.tell(new Info(text, name), getSelf()); 
    }

    @Override
    public void onReceive(Object msg) throws Exception
    {
    		if (msg instanceof Change)
    		{
    			String nr = ((Change) msg).getNumber();
    			String text = "misja " + nr;
    			table.tell(new Info(text,name), getSelf());
    			table.tell(new Change(nr,name), getSelf());
    		}
    		else if (msg instanceof Message)	
            {   
            	String nr = ((Message) msg).getMessage(); 
            /*	if (nr.contains("change")){
            		String cardsChange = nr.substring(6);
            		String[] changeArray = cardsChange.split(" ");
            		int intChangeArray[] = new int [changeArray.length]; 
            		for(int i=0; i < changeArray.length ; i++){
            			intChangeArray[i] = Integer.parseInt(changeArray[i]);
            		table.tell(new Info("" + intChangeArray[i], name), getSelf());
            		}*/
            	
            		
            	
            	table.tell(new Message(nr, name,totalCash), getSelf());
            }  
    		else if (msg instanceof PrepareRound){
    			if(totalCash > entryFee){
    				totalCash -= entryFee;
    				table.tell(new Entry(false, name, totalCash), getSelf());
    			}
    			//else table.tell(new Bankrupt(), getSelf());
    		}
    		else if( msg instanceof ReqName){
    			table.tell(new GetName(name),getSelf());
    		}
            else if(msg instanceof Info)
            {        
            	Info info = (Info) msg;             	
	            ObjectNode event = Json.newObject();
	            event.put("message", "[ "+ info.getName()+ " ] : " + info.getText()); 
	            
	            out.write(event);
            }
            else if(msg instanceof InitGame){
            	InitGame initGame = (InitGame) msg;
            	entryFee = initGame.getEntryFee();
            	totalCash = initGame.getStartCash();
            }
            else if(msg instanceof SendHand) {
            	SendHand sendHand = (SendHand) msg;
            	hand  = sendHand.getHand();
            	
            }
            else if (msg instanceof GetCash) {
            	GetCash myCash = (GetCash) msg;
            	totalCash -= myCash.getCash();
            	
            }
            else if (msg instanceof SendCard){
            	SendCard sendCard = (SendCard) msg;
            	hand.cards[sendCard.getCardIndex()] = sendCard.getCard();
            }

            	
            
            else {
                unhandled(msg);
            }
            
        }  
            
}

